package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.PageInf;
import com.cykj.v_m_sprintboot.bean.TbCommodity;
import com.cykj.v_m_sprintboot.bean.TbCommorityCategory;
import com.cykj.v_m_sprintboot.bean.TbVmi;
import com.cykj.v_m_sprintboot.service.TbCommodityCategoryService;
import com.cykj.v_m_sprintboot.service.TbCommodityService;
import com.cykj.v_m_sprintboot.service.TbVmiService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

import com.cykj.v_m_sprintboot.util.*;
/**
 * 艾彬
 * 2021/09/30
 */
@RestController
public class Commority {
    private String fileImgUrl;
    @Autowired
    private TbCommodityService tbCommodityService;
    @Autowired
    private TbCommodityCategoryService tbCommodityCategoryService;
    @Autowired
    private TbVmiService tbVmiService;


    @RequestMapping("/selpage")
    public Map<String,Object> selectvm(PageInf pageInf,String vmid,String cateid){
        int page = (pageInf.getPage()-1)* pageInf.getSize();
        int size = pageInf.getSize();
        int aaa;
        int bbb;
        if (vmid==null){//判断传过来string值为空转成0
            aaa = 0;
        }else{
            aaa = Integer.valueOf(vmid);
        }
        if (cateid==null){//判断传过来string值为空转成0
            bbb = 0;
        }else{
            bbb = Integer.valueOf(cateid);
        }
        if (cateid==null){
        }
        System.out.println(page+";"+size+"=------"+aaa+"-----------"+bbb);

        Map<String,Object> map = new HashMap<>();
        int count = tbCommodityService.selcommdity();//商品总条数
        List<TbCommorityCategory> commcategoryList = tbCommodityCategoryService.categorylist();//商品类别
        List<TbCommodity> tbcommdityList = tbCommodityService.selcommdityPage(page,size,aaa,bbb);//商品分页
        int cateCount = commcategoryList.size();
        int commCount = tbcommdityList.size();
        map.put("vimList",tbVmiService.VM());//售货机
        map.put("commcategoryList",commcategoryList);//商品类别
        map.put("count",count);
        map.put("cateCount",cateCount);
        map.put("commCount",commCount);
        map.put("tbcommdityList",tbcommdityList);//商品分页
        System.out.println(map);
        return map;
    }

    @RequestMapping("/menu")
    public List menulist(String id){//小程序查询商品菜单
        System.out.println("123=="+id);
        Map<String,Object> menumap = new HashMap<>();
        List<TbCommorityCategory> tbcommcateList = tbCommodityCategoryService.categorylist();
        System.out.println(tbcommcateList.size()+"111111111111111");
        menumap.put("tbcommcateList",tbcommcateList);
        return tbcommcateList;
    }

    @RequestMapping("/selectComm")
    public Map<String,Object> selectComm(String page,String size, String vmid, String cateid){//查询
        int page1 = Integer.valueOf(page);
        int size1 = Integer.valueOf(size);
        System.out.println(vmid+"-==09-"+cateid+"-90-"+"==0=="+page+"08---"+size);
        //查询售货机商品
        List<TbCommodity> tbcommList = tbCommodityService.selectCommList(Integer.valueOf(vmid),Integer.valueOf(cateid),page1,size1);
        System.out.println(tbcommList.size());
        Map<String,Object> commmap = new HashMap<>();
        commmap.put("tbcommList",tbcommList);
        return commmap;
    }

    @RequestMapping("/updComm")
    public String updcomm(String comNum,String comId,String comName,String comPrice){//修改商品数量
        System.out.println(comNum+"--"+comId+comName+",--"+comPrice);
        tbCommodityService.updateCommodity(Integer.valueOf(comNum),Integer.valueOf(comId),comName,comPrice);
        return "新增成功";
    }

    @RequestMapping("/delComm")
    public String delcomm(String comId){//删除商品
        System.out.println("--"+comId);
        tbCommodityService.delCommodity(Integer.valueOf(comId));
        return "删除成功";
    }

    @RequestMapping("/showcomm")
    public Map<String, Object> showcomm(String vmid){//前端商品
        System.out.println("售货机id："+vmid);
        Map<String,Object> qdCommMap = new HashMap<>();
        Map<String,Object> qmap = new HashMap<>();

        List<TbCommorityCategory> selcommcateList = tbCommodityCategoryService.selCommCatList(Integer.valueOf(vmid));
        for (int i=0;i<selcommcateList.size();i++){
            System.out.println(selcommcateList.get(i)+"-----0910");
        }
        qmap.put("selcommcateList",selcommcateList);
        return qmap;
    }

    @PostMapping(value = "/upImg")//文件上传ftp
    @ResponseBody
    public Object upImg(HttpServletRequest request, @RequestParam("fileName") MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        //获取上传的文件流
        InputStream inputStream = file.getInputStream();
        //获取上传的文件名
        String filename = file.getOriginalFilename();
        double fileSize = file.getSize();
        double fileSizeMB = new BigDecimal(fileSize/1024/1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("后台床过来的名字:"+filename);
        System.out.println("文件长度为："+fileSizeMB);
        //截取后缀
        String suffix = filename.substring(filename.lastIndexOf("."));
        //使用UUID拼接后缀，定义一个不重复的文件名
        String finalname = UUID.randomUUID()+suffix;

        //调用自定义的FTP工具类上传文件
        boolean fing = FtpUtil.uploadFile(finalname,inputStream);
        fileImgUrl = "121.43.147.36/"+finalname;
        System.out.println("名字是多少"+fileImgUrl);
        if (fing==true){
//            securityVideo.setVideoName(filename);
//            securityVideo.setVideoSize(fileSizeMB);
//            securityVideo.setVideoUrl(finalname);
//            securityVideo.setCreationTime(df.format(new Date()));
//            lpwVideoService.videoadd(securityVideo);
            System.out.println("添加成功");
//            return "上传成功";
//        }else{
//            return "文件上传失败";
        }
        return "1";
    }

    @RequestMapping("/inscomm")
    public String insComm(String vmid,String cateid,String comName,String comPrice,String comNum){

        String comImage = fileImgUrl;
        System.out.println(vmid+cateid+comName+comPrice+comNum+comImage);
        tbCommodityService.insComm(comImage,comName,comPrice,Integer.valueOf(cateid),Integer.valueOf(vmid),Integer.valueOf(comNum));
        System.out.println("成功");
        return "1";
    }
}
